package com.ztewatch.wearcontacts;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ztewatch.wearcontacts.utils.ConfigLoaderSelectionUtils;
import com.ztewatch.wearcontacts.utils.ContactQuery;
import com.ztewatch.wearcontacts.view.ContactCursorAdapter;
import com.ztewatch.wearcontacts.view.ScrollGridView;

public class WearContactGroupDetailEditActivity extends Activity {
	public static final String GROUP_DETAIL_FALG = "group_detail_flag";
	private Uri groupUri;
	private ScrollGridView mDetailScrollGridView;
	// private ScrollGridView base_gridview;
	private ContactCursorAdapter contactCursorAdapter;

	// private List<HashMap<String, Object>> baseDataList = new
	// ArrayList<HashMap<String, Object>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wear_contact_group_detail_edit);

		groupUri = getIntent().getData();

		initView();
		// initBaseGridView();
	}

	private void initView() {
		mDetailScrollGridView = (ScrollGridView) findViewById(R.id.group_detail_edit_gridview);
		contactCursorAdapter = new ContactCursorAdapter(this, null);
		mDetailScrollGridView.setAdapter(contactCursorAdapter);
		mDetailScrollGridView
				.setOnItemClickListener(new ContactItemClickListener());
	}

	private class ContactItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Cursor cursor = (Cursor) contactCursorAdapter.getItem(position);
			if (cursor != null) {
				Uri contactUri = getContactUri(cursor);
				if (contactUri != null) {// contact
					Intent intent = new Intent(
							WearContactGroupDetailEditActivity.this,
							WearContactEditActivity.class);
					intent.setData(contactUri);
					startActivity(intent);
				}
			}
		}

	}

	public Uri getContactUri(Cursor cursor) {
		Uri uri = null;
		int id_index = cursor.getColumnIndex(Contacts._ID);
		int lookup_key_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
		if (id_index != -1 && lookup_key_index != -1) {
			long contactId = cursor.getLong(id_index);
			String lookupKey = cursor.getString(lookup_key_index);
			uri = Contacts.getLookupUri(contactId, lookupKey);
		}
		return uri;
	}

	private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

		@Override
		public void onLoaderReset(Loader<Cursor> arg0) {

		}

		@Override
		public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
			contactCursorAdapter.swapCursor(arg1);
		}

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			CursorLoader cursorLoader = new CursorLoader(
					WearContactGroupDetailEditActivity.this);
			Uri uri = Contacts.CONTENT_URI;
			cursorLoader.setUri(uri);
			cursorLoader.setProjection(ContactQuery.CONTACT_PROJECTION);
			if (groupUri != null) {
				long groupId = ContentUris.parseId(groupUri);
				if (groupId > 0)
					ConfigLoaderSelectionUtils.configGroupSelection(
							cursorLoader, 0, groupId);
			}
			return cursorLoader;
		}
	};

	@Override
	protected void onStart() {
		super.onStart();
		getLoaderManager().initLoader(0, null, loaderCallbacks);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getLoaderManager().destroyLoader(0);
	}
}
